//== AUTOMATICALLY GENERATED =========================================================

#include "sagittarius/core/SagittariusCore.h"
#include "sagittarius/lexer/lexcore.h"

#include "sagittarius/io/io.h"

//== BEGINNING OF CODE ===============================================================


#define SM_START_STATE 0
#define DFA_ACC_NONE 0xffffffff
#define STATE_INFO_SHIFT_MASK 0x00000000000000ff
#define STATE_INFO_OFFSET_MASK 0xffffffff00000000


#define SM_ITABLE_CONTINUE_FLAG 0x10000000
#define SM_LOOKUP_MASK 0x0fffffff
#define DFA_INVALID_TRANSITION 0xffff0000





#define TOKEN_ws 0
#define TOKEN_id 1
#define TOKEN_lparen 2
#define TOKEN_rparen 3



uint64 state_info[5] = {
  0x0000000000000000, 0x0000000000000000, 0x0000010000000000, 0x0000000000000000,
  0x0000000000000000};



typedef struct {
    constLexBuf w; /* Position in the input buf */
    constLexBuf strt; /* Starting position of accepted pattern (where ac = true) */
    uint32 s; /* State */
    uint32 ef; /* Error flag */
    uint32 ac; /* Accepted pattern */
} sm_c_parserState;



void dfa_init_ps(sm_c_parserState* ps, constLexBuf buf) {
    /* Initialize the parser state */
    ps->s = 0;
    ps->w = buf;
    ps->strt = ps->w;
    ps->ac = DFA_ACC_NONE;
    ps->ef = 0;
}



const char* identifiers[4] = {
  "ws",
  "id",
  "lparen",
  "rparen"
};

void dfa_print_acc_id(uint32 ac) {
    sg_writeCStr(1, identifiers[ac]);
}







void init_symbols() {
}



void clear_symbols() {
}



uint32 ttbl[512] = {
  /* Transitions for tier 1, state 0*/
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x10000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x10000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x10000003, 0x10000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x10000002,
  0x00000000, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  /* Transitions for tier 1, state 2*/
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x10000002,
  0x00000000, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002, 0x10000002,
  0x10000002, 0x10000002, 0x10000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
};


uint32 lookup_transition(uint32 s, sm_c_parserState* ps) {
    LXAlphaType x;
    LX_FORCE_INIT(x);
    
    uint32 shift=state_info[s] & STATE_INFO_SHIFT_MASK;
    LXFixedType base=(uint32)((state_info[s] & STATE_INFO_OFFSET_MASK) >> 32);
    while(shift) {
        LX_ALPHA_SET(x, *ps->w);
        LX_ALPHA_RSHIFT_SPC(x, (8*shift));
        base = ttbl[base + (LXFixedType)(LX_GET_FIXED_VALUE(x) & 0xff)];
        if(base & SM_ITABLE_CONTINUE_FLAG)
            base = base & SM_LOOKUP_MASK;
        else
            return DFA_INVALID_TRANSITION;
        --shift;
    }
    /* Get next state */
    LX_ALPHA_SET(x, *ps->w);
    base = ttbl[base + (LXFixedType)(LX_GET_FIXED_VALUE(x) & 0xff)];
    
    LX_ALPHA_CLEAR(x);
    
    if(base & SM_ITABLE_CONTINUE_FLAG)
        base = base & SM_LOOKUP_MASK;
    else
        return DFA_INVALID_TRANSITION;
    
    return (uint32)base & SM_LOOKUP_MASK;
}


void resume_dfa(sm_c_parserState* ps) {
    uint32 s;
    
    while(!LX_CMPALPHA_EQ_SPC(*ps->w, '\0') && !ps->ef) {
        switch(ps->s) {
          case 0:
            /* State 0 has transitions: CS{ ,0xa} -> 1, CS{a-z,_,A-Z} -> 2, CS{(} -> 3, CS{)} -> 4 */
            s = lookup_transition(ps->s, ps);
            if(s != DFA_INVALID_TRANSITION) {
                ps->s = s;
                ++ps->w;
                break;
            }
            /* Reject if there are no legal transitions */
            ps->ef = 1;
            return;
          case 1:
            /* State 1 has no outbound transitions*/
            /* Accepting state for "ws" */
            /* Accept pattern 0 */
            /* Reset */
            ps->s = 0;
            ps->ac = DFA_ACC_NONE;
            ps->strt = ps->w;
            break;
          case 2:
            /* State 2 has transitions: CS{0-9,A-Z,_,a-z} -> 2 */
            /* Accepting state for "id" */
            s = lookup_transition(ps->s, ps);
            if(s != DFA_INVALID_TRANSITION) {
                ps->s = s;
                ++ps->w;
                break;
            }
            /* Accept pattern 1 */
            ps->ac = 1;
            return;
          case 3:
            /* State 3 has no outbound transitions*/
            /* Accepting state for "lparen" */
            /* Accept pattern 2 */
            ps->ac = 2;
            return;
          case 4:
            /* State 4 has no outbound transitions*/
            /* Accepting state for "rparen" */
            /* Accept pattern 3 */
            ps->ac = 3;
            return;
        }
    }
}


